package cn.iocoder.yudao.module.erp.service.business.group.goodscategory;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategoryPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategorySaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.group.goodscategory.GoodsCategoryDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.group.goodscategory.GoodsCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 物品类目 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper categoryMapper;

    @Override
    public Long createCategory(GoodsCategorySaveReqVO createReqVO) {
        // 插入
        GoodsCategoryDO category = BeanUtils.toBean(createReqVO, GoodsCategoryDO.class);
        // 校验类目名的唯一性
        validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
        categoryMapper.insert(category);
        // 返回
        return category.getId();
    }


    @Override
    public void updateCategory(GoodsCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        // 校验类目名的唯一性
        validateDeptNameUnique(null, updateReqVO.getParentId(), updateReqVO.getName());
        // 更新
        GoodsCategoryDO updateObj = BeanUtils.toBean(updateReqVO, GoodsCategoryDO.class);
        categoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验存在
        validateCategoryExists(id);
        // 校验是否有子部门
        if (categoryMapper.selectCountByParentId(id) > 0) {
            throw exception(CATEGORY_EXISTS_SON);
        }
        // 删除
        categoryMapper.deleteById(id);
    }

    private void validateDeptNameUnique(Long id, Long parentId, String name) {
        GoodsCategoryDO category = categoryMapper.selectByParentAndName(parentId, name);
        if (category == null) {
            return;
        }
        if (id == null) {
            throw exception(CATEGORY_NAME_DUPLICATE);
        }
        if (ObjectUtil.notEqual(category.getId(), id)) {
            throw exception(CATEGORY_NAME_DUPLICATE);
        }
    }


    private void validateCategoryExists(Long id) {
        if (categoryMapper.selectById(id) == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public GoodsCategoryDO getCategory(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<GoodsCategoryDO> getCategoryPage(GoodsCategoryPageReqVO pageReqVO) {
        return categoryMapper.selectPageList(pageReqVO);
    }

    @Override
    public List<GoodsCategoryDO> getCategoryList() {
        return categoryMapper.selectList();
    }

}